package com.supcon.exchangedata.repository.master;

import com.supcon.exchangedata.pojo.entity.oracle.StatistOtherParametersDay;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yangbin
 * @Date: 2022/03/18/9:36
 * @Description:
 */
public interface MasterStatistOtherParametersDayRepository extends JpaRepository<StatistOtherParametersDay, Long> {
    public StatistOtherParametersDay findByObjIdAndTypeAndDayOfMonthKey(Long objId, Long type, LocalDateTime dayOfMonthKey);


    @Transactional
    @Modifying(clearAutomatically = true)
    @Query("update StatistOtherParametersDay s set s.inAvgValue = ?1 where s.objId =?2 and s.dayOfMonthKey =?3 and s.type = ?4")
    public Integer updateDay(Double currentValue, Long objId, LocalDateTime dayOfMonthKey, Long type);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(nativeQuery = true, value = "insert into STATIST_OTHER_PARAMETERS_DAY(ID,OBJ_ID,TYPE,DAY_OF_MONTH_KEY,IN_AVG_VALUE,OBJ_TYPE,LAST_TIME) values (?1,?2,?3,?4,?5,?6,?7)")
    public Integer insertDay(int id, Long objId, Long type, LocalDateTime dayOfMonthKey, Double InAvgValue, Long objType, LocalDateTime lastTime);


    StatistOtherParametersDay findTopByObjIdAndTypeOrderByDayOfMonthKeyDesc(Long objId,Long type);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(nativeQuery = true, value = "select * from STATIST_OTHER_PARAMETERS_DAY where TYPE = ?1 AND DAY_OF_MONTH_KEY IS NOT NULL and LAST_TIME IS NOT NULL AND OBJ_ID = 3")
    List<StatistOtherParametersDay> findByType(String type);

    StatistOtherParametersDay getByDayOfMonthKeyAndType(String day,String type);


    StatistOtherParametersDay findDistinctByDayOfMonthKeyAndObjIdAndType(LocalDateTime dayOfMonthKey, Long objId, Long type);

}
